package cn.knightzz.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysPopulation;
import cn.knightzz.service.SysPopulationService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-09
 */
@RestController
public class SysPopulationController {

    @Resource
    SysPopulationService populationService;

    @PostMapping("/population")
    public JsonResult<Object> addPopulation(@RequestBody SysPopulation population) {
        Boolean status = populationService.addPopulation(population);
        if (status) {
            return new JsonResult<>("200", "添加成功!");
        } else {
            return new JsonResult<>("201", "添加失败!");
        }
    }

    @PutMapping("/population")
    public JsonResult<Object> editPopulation(@RequestBody SysPopulation population) {
        Boolean status = populationService.updatePopulation(population);
        if (status) {
            return new JsonResult<>("200", "更新成功!");
        } else {
            return new JsonResult<>("201", "更新失败!");
        }
    }

    @DeleteMapping("/population/{id}")
    public JsonResult<Object> deletePopulation(@PathVariable Long id) {
        Boolean status = populationService.deletePopulation(id);
        if (status) {
            return new JsonResult<>("200", "删除成功!");
        } else {
            return new JsonResult<>("201", "删除失败!");
        }
    }

    @GetMapping("/population/{id}")
    public JsonResult<HashMap<String, Object>> selectByPage(@PathVariable("id") Long userId,
                                                            @RequestParam Long pageNum,
                                                            @RequestParam Long pageSize,
                                                            @RequestParam String query) {
        IPage<SysPopulation> pages = populationService.selectByPage(userId,pageNum, pageSize, query);
        long total = pages.getTotal();
        List<SysPopulation> records = pages.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        JsonResult<HashMap<String, Object>> result;

        if (ObjectUtil.isNotNull(pages)) {
            data.put("total", total);
            data.put("population", records);
            result = new JsonResult<>("200", "查询成功");
            result.setData(data);
        } else {
            // 查询异常, 返回NULL
            data.put("total", 0);
            data.put("population", new ArrayList<>());
            result = new JsonResult<>("201", "查询失败");
            result.setData(data);
        }
        return result;
    }

    @GetMapping("/population/area")
    public JsonResult<List<SysPopulation>> getPopulationByArea(@RequestParam("area") String area,
                                                         @RequestParam("query") String query){
        List<SysPopulation> populationByArea = populationService.getPopulationByArea(area, query);
        JsonResult<List<SysPopulation>> result = new JsonResult<>("200", "查询成功!");
        result.setData(populationByArea);
        return result;
    }
}

